package com.mandao.concurrency.example.singleton;

import com.mandao.concurrency.annotations.Recommend;
import com.mandao.concurrency.annotations.ThreadSafe;

/**
 *  使用枚举实现单例
 */
@ThreadSafe
@Recommend
public class SingletonExample7 {

    private SingletonExample7(){}

    private static SingletonExample7 instance = null;

    public static SingletonExample7 getInstance(){
        return Singleton.init.getInstance();
    }

    private enum Singleton{
        init;

        private SingletonExample7 singleton;

        /**
          JVM保证了只调用一次 而且时绝对的  所有是线程安全的
         */
        Singleton(){
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getInstance(){
            return  singleton;
        }

        public SingletonExample7 getSingleton(){
            return singleton;
        }
    }
}
